﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestSystem.Domain.Entities;
using TestSystem.Domain.Entities.Authorisation;
using TestSystem.Domain.Entities.Position;
using TestSystem.Domain.Entities.Questions;
using TestSystem.Security.Hash;

namespace TestSystem.Domain.DAL
{
    public class Initializer : DropCreateDatabaseIfModelChanges<TestSystemContext>
    {
        protected override void Seed(TestSystemContext context)
        {
            var dificulties = new List<Difficulty>
                                  {
                                      new Difficulty() {DifficultyID = 1, DifficultyName = "Łatwy"},
                                      new Difficulty() {DifficultyID = 2, DifficultyName = "Średni"},
                                      new Difficulty() {DifficultyID = 3, DifficultyName = "Trudny"}
                                  };
            dificulties.ForEach(c => context.Difficulties.Add(c));
            context.SaveChanges();

            var categorie = new List<Category>
                                {
                                    new Category() {CategoryID = 1, Name = "Programowanie"},
                                    new Category() {CategoryID = 2, Name = "Bazy danych"},
                                    new Category() {CategoryID = 3, Name = "Algorytmy"}
                                };
            categorie.ForEach(c => context.Categories.Add(c));
            context.SaveChanges();

            var questionType = new List<QuestionType>
                                   {
                                       new QuestionType() {QuestionTypeID = 1, Type = "Otwarte"},
                                       new QuestionType() {QuestionTypeID = 2, Type = "Zamknięte"}
                                   };
            questionType.ForEach(c => context.QuestionTypes.Add(c));
            context.SaveChanges();

            var question = new List<Question>
                               {
                                   new Question()
                                       {
                                           QuestionID = 1,
                                           QuestionText = "pytanie pytanie pytanie",
                                           QuestionTypeID = 1,
                                           CategoryID = 1,
                                           DifficultyID = 1
                                       }
                               };
            question.ForEach(c => context.Questions.Add(c));
            context.SaveChanges();

            var position = new List<Position>
                               {
                                   new Position()
                                       {
                                           PositionID = 1,
                                           PositionName = "Software Developer"
                                       }
                               };
            position.ForEach(position1 => context.Positions.Add(position1));
            context.SaveChanges();

            var questionTypeToPosition = new List<QuestionTypeToPosition>
                                             {
                                                 new QuestionTypeToPosition()
                                                     {
                                                         PositionID = 1,
                                                         QuestionTypeToPositionID = 1,
                                                         QuestionTypeID = 1,
                                                         Quantity = 2
                                                     },
                                                 new QuestionTypeToPosition()
                                                     {
                                                         PositionID = 1,
                                                         QuestionTypeID = 2,
                                                         Quantity = 5,
                                                         QuestionTypeToPositionID = 2
                                                     }
                                             };
            questionTypeToPosition.ForEach(toPosition => context.QuestionTypeToPositions.Add(toPosition));
            context.SaveChanges();

            var questionDifficultyToPosition = new List<QuestionDifficultyToPosition>
                                                   {
                                                       new QuestionDifficultyToPosition()
                                                           {
                                                               QuestionDifficultyToPositionID = 1,
                                                               PositionID = 1,
                                                               DifficultyID = 2
                                                           }
                                                   };
            questionDifficultyToPosition.ForEach(toPosition => context.QuestionDifficultyToPositions.Add(toPosition));
            context.SaveChanges();

            var categoryToPosition = new List<CategoryToPosition>
                                         {
                                             new CategoryToPosition()
                                                 {
                                                     PositionID = 1,
                                                     CategoryID = 1,
                                                     CategoryToPositionID = 1
                                                 },
                                             new CategoryToPosition()
                                                 {
                                                     PositionID = 1,
                                                     CategoryID = 2,
                                                     CategoryToPositionID = 2
                                                 }
                                         };
            categoryToPosition.ForEach(toPosition => context.CategoryToPositions.Add(toPosition));
            context.SaveChanges();

            var roles = new List<Role>
                            {
                                new Role
                                    {
                                        RoleId = 1,
                                        RoleName = "Administrator"
                                    },
                                new Role
                                    {
                                        RoleId = 2,
                                        RoleName = "Checker"
                                    },
                                new Role
                                    {
                                        RoleId = 3,
                                        RoleName = "User"
                                    }
                            };

            roles.ForEach(role => context.Roles.Add(role));
            context.SaveChanges();

            var users = new List<User>
                            {
                                new User
                                    {
                                        UserId = 1,
                                        Username = "admin",
                                        Password = Hashes.HashPassword("adminadmin["),
                                        Email = "admin@admin.pl",
                                        Roles = new Collection<Role>{roles.First()},
                                        CreateDate = DateTime.Now,
                                        IsApproved = true,
                                        IsLockedOut = false,
                                        PasswordFailuresSinceLastSuccess = 0
                                    }
                            };
            users.ForEach(user => context.Users.Add(user));
            context.SaveChanges();
        }
    }
}
